﻿'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  console.log('=== testSelfLike 开始 ===');
  console.log('接收参数:', event);
  
  try {
    const { userId, postId, postTitle } = event;
    
    if (!userId || !postId) {
      return {
        code: 400,
        msg: '参数缺失',
        data: { userId, postId }
      };
    }
    
    console.log('1. 测试自点赞逻辑');
    console.log('2. 用户ID:', userId);
    console.log('3. 帖子ID:', postId);
    
    // 模拟自点赞场景：userId === receiverId
    const testData = {
      userId: userId,
      postId: postId,
      receiverId: userId, // 关键：接收者就是点赞者自己
      postTitle: postTitle || '测试帖子'
    };
    
    console.log('4. 测试数据:', testData);
    
    // 调用 handleLike 云函数
    const result = await uniCloud.callFunction({
      name: 'handleLike',
      data: testData
    });
    
    console.log('5. handleLike 返回结果:', result);
    
    if (result.result && result.result.success) {
      if (result.result.skipMessage) {
        return {
          code: 200,
          msg: '自点赞逻辑测试成功',
          data: {
            userId: userId,
            postId: postId,
            skipMessage: true,
            message: result.result.message
          }
        };
      } else {
        return {
          code: 200,
          msg: '点赞成功，但未跳过消息生成',
          data: {
            userId: userId,
            postId: postId,
            skipMessage: false,
            message: result.result.message
          }
        };
      }
    } else {
      return {
        code: 500,
        msg: '测试失败: ' + (result.result?.message || '未知错误'),
        data: result.result
      };
    }
    
  } catch (error) {
    console.error(' testSelfLike 异常:', error);
    return {
      code: 500,
      msg: '测试失败: ' + error.message,
      error: error
    };
  }
};
